package com.turing.system.service.impl;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.turing.system.entity.Customer;
import com.turing.system.entity.Message;
import com.turing.system.entity.MyMessage;
import com.turing.system.mapper.CustomerMapper;
import com.turing.system.mapper.MyMessageMapper;
import com.turing.system.service.MessageService;
import com.turing.system.mapper.MessageMapper;
import enums.ErrorCode;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import result.Result;

import javax.annotation.Resource;

/**
* @author dell
* @description 针对表【message(消息)】的数据库操作Service实现
* @createDate 2023-05-08 21:24:57
*/
@Service
public class MessageServiceImpl extends ServiceImpl<MessageMapper, Message>
    implements MessageService{

    @Resource
    private MyMessageMapper myMessageMapper;

    @Override
    public Result addSave(JSONObject jsonObject) throws ParseException {
        String messageTitle = jsonObject.getString("messageTitle");
        String messageContent = jsonObject.getString("messageContent");
        JSONArray customerIds = jsonObject.getJSONArray("customerIds");
        System.out.println("customerIds = " + customerIds);
        String startTime = jsonObject.getString("startTime");
        String endTime = jsonObject.getString("endTime");
        String commentId = jsonObject.getString("commentId");
        Integer messageType = jsonObject.getInteger("messageType");
        String cusid = "";
        if(customerIds != null){
            for (int i = 0; i < customerIds.size(); i++) {
                cusid += ","+ customerIds.getString(i);
            }
        }

        //用户ids不为null
        if(customerIds != null){
            System.out.println("1212121");
            //添加信息表
            Message message = new Message();
            message.setMessageId(UUID.randomUUID().toString());
            message.setMessageTitle(messageTitle);
            message.setMessageContent(messageContent);
            message.setCustomerIds(cusid.substring(1));
            message.setStartTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(startTime));
            message.setEndTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(endTime));
            if(commentId != null && commentId != ""){
                message.setCommentId(commentId);
            }
            message.setMessageState(0);
            message.setMessageType(messageType);
            this.baseMapper.insert(message);
            //添加我的消息表
            for (int i = 0; i < customerIds.size(); i++) {
                MyMessage myMessage = new MyMessage();
                myMessage.setMmId(UUID.randomUUID().toString());
                myMessage.setCusId(customerIds.get(i).toString());
                myMessage.setMessageId(message.getMessageId());
                myMessage.setMmState(0);
                myMessageMapper.insert(myMessage);
            }
            return Result.success();
        //用户ids为null
        }else {
            //添加信息表
            Message message = new Message();
            message.setMessageId(UUID.randomUUID().toString());
            message.setMessageTitle(messageTitle);
            message.setMessageContent(messageContent);
            if(customerIds != null){
                message.setCustomerIds(cusid.substring(1));
            }

            message.setStartTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(startTime));
            message.setEndTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(endTime));
            if(commentId != null && commentId != ""){
                message.setCommentId(commentId);
            }
            message.setMessageState(0);
            message.setMessageType(messageType);
            this.baseMapper.insert(message);
            //添加我的消息表
            List<String> cusIds = this.baseMapper.selectId();

            List<MyMessage> list = new ArrayList<>();
            for (int i1 = 0; i1 < cusIds.size(); i1++) {
                MyMessage myMessage = new MyMessage();
                myMessage.setMmId(UUID.randomUUID().toString());
                myMessage.setCusId(cusIds.get(i1));
                myMessage.setMessageId(message.getMessageId());
                myMessage.setMmState(0);
                list.add(myMessage);

            }
            myMessageMapper.insertMyMi(list);
            return Result.success();
        }

    }

    @Override
    public Result msgEdit(JSONObject jsonObject) throws ParseException {
        String messageId = jsonObject.getString("messageId");
        String messageTitle = jsonObject.getString("messageTitle");
        String messageContent = jsonObject.getString("messageContent");
        JSONArray customerIds = jsonObject.getJSONArray("customerIds");
        String startTime = jsonObject.getString("startTime");
        String endTime = jsonObject.getString("endTime");
        Message message = JSONObject.toJavaObject(jsonObject, Message.class);
        System.out.println("customerIds = " + customerIds);

        Message message1 = this.baseMapper.selectById(messageId);
        if(message1 == null){
            return Result.fail(ErrorCode.NODATA);
        }
        System.out.println("messageId = " + messageId);
        System.out.println("message1 = " + message1);
        System.out.println("message1.getStartTime() = " + message1.getStartTime());
        if(message1.getStartTime().before(new Date())){
            return Result.fail(2,"此消息已开始");
        }
        //删除我的消息表
        LambdaQueryWrapper<MyMessage> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(MyMessage::getMessageId,messageId);
        myMessageMapper.delete(wrapper);

        //修改消息
        int i = this.baseMapper.updateById(message);
        System.out.println("message1 = " + message1);
        //添加我的消息表
        if(customerIds != null){
            for (int i1 = 0; i1 < customerIds.size(); i1++) {
                MyMessage myMessage = new MyMessage();
                myMessage.setMmId(UUID.randomUUID().toString());
                myMessage.setCusId(customerIds.get(i1).toString());
                myMessage.setMessageId(messageId);
                myMessage.setMmState(0);
                myMessageMapper.insert(myMessage);
            }
        }else {
//            List<String> cusIds = this.baseMapper.selectId();
            myMessageMapper.insertAll(messageId);
//            for (int i1 = 0; i1 < cusIds.size(); i1++) {
//                MyMessage myMessage = new MyMessage();
//                myMessage.setMmId(UUID.randomUUID().toString());
//                myMessage.setCusId(cusIds.get(i1));
//                myMessage.setMessageId(messageId);
//                myMessage.setMmState(0);
//                myMessageMapper.insert(myMessage);

//            }
        }


        if(i > 0){
            return Result.success();
        }
        return Result.fail(ErrorCode.ERROR_EDIT);
    }


}




